﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using AdaptiveAgents.Experiments;
using AdaptiveAgents.Loggers;

namespace GameAnalyzer
{
    public partial class frmExperiments : Form
    {
        private DataSet _dsExperiments = null;
        private readonly string EXPERIMENTS_TABLE = "Experiments";
        private readonly string DATA_FILE = "../../Data/ExperimentsData.xml";
        private readonly string SCHEMA_FILE = "../../Data/ExperimentsSchema.xml";
        private readonly int EXPERIMENT = 0;
        private readonly int DETAILS = 1;
        private readonly int COMMENTS = 2;

        //private List<DataSet> _customGames = null;
        private int _gameNameCounter = 0;

        public frmExperiments()
        {
            InitializeComponent();
        }

        /*private void createGamesGridNOT_WORKING()
        {
            //Create Date column
            DataGridViewTextBoxColumn clnDate = new DataGridViewTextBoxColumn();
            clnDate.HeaderText = "Date";
            clnDate.Name = "Date";
            clnDate.ReadOnly = true;
            clnDate.SortMode = DataGridViewColumnSortMode.NotSortable;
            clnDate.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dgGames.Columns.Add(clnDate);

            //Create game details file button column
            DataGridViewButtonColumn clnDetails = new DataGridViewButtonColumn();
            clnDetails.HeaderText = "Details";
            clnDetails.Name = "Details";
            clnDetails.ReadOnly = true;
            clnDetails.SortMode = DataGridViewColumnSortMode.NotSortable;
            clnDetails.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dgGames.Columns.Add(clnDetails);

            //Create Comments column
            DataGridViewTextBoxColumn clnComments = new DataGridViewTextBoxColumn();
            clnComments.HeaderText = "Comments";
            clnComments.Name = "Comments";
            clnComments.ReadOnly = true;
            clnComments.SortMode = DataGridViewColumnSortMode.NotSortable;
            clnComments.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dgGames.Columns.Add(clnComments);
        }*/

        private void createGamesGrid()
        {
            //Create Name column
            DataGridViewTextBoxColumn clnName = new DataGridViewTextBoxColumn();
            clnName.HeaderText = "Name";
            clnName.Name = "Name";
            clnName.ReadOnly = true;
            clnName.SortMode = DataGridViewColumnSortMode.NotSortable;
            clnName.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dgvGames.Columns.Add(clnName);

            //Create game details file button column
            DataGridViewButtonColumn clnDetails = new DataGridViewButtonColumn();
            clnDetails.HeaderText = "Details";
            clnDetails.Name = "Details";
            clnDetails.ReadOnly = true;
            clnDetails.SortMode = DataGridViewColumnSortMode.NotSortable;
            clnDetails.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dgvGames.Columns.Add(clnDetails);

            //Create Comments column
            DataGridViewTextBoxColumn clnComments = new DataGridViewTextBoxColumn();
            clnComments.HeaderText = "Comments";
            clnComments.Name = "Comments";
            clnComments.ReadOnly = true;
            clnComments.SortMode = DataGridViewColumnSortMode.NotSortable;
            clnComments.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvGames.Columns.Add(clnComments);
        }

        /*private string getExperimentsSchema()
        {
            _dsExperiments.Tables.Add("Experiments");
            _dsExperiments.Tables["Experiments"].Columns.Add(EXPERIMENT, typeof(string));
            _dsExperiments.Tables["Experiments"].Columns.Add(DATE, typeof(DateTime));
            _dsExperiments.Tables["Experiments"].Columns.Add(DETAILS_FILE, typeof(string));
            _dsExperiments.Tables["Experiments"].Columns.Add(COMMENTS, typeof(string));

            return _dsExperiments.GetXmlSchema();
        }*/

        private void frmExperiments_Load(object sender, EventArgs e)
        {
            

            //Read data set schema 
            _dsExperiments = new DataSet("GAMES");
            //_dsExperiments.ReadXmlSchema(SCHEMA_FILE);
            _dsExperiments.ReadXml(DATA_FILE);

            //Create grid and load data from xml file
            createGamesGrid();
            //createGamesGrid();


            //Fill experiment grid with data
            //fillExperimentData();

            //Add a CellClick handler to handle clicks in the button column
            dgvGames.CellClick += new DataGridViewCellEventHandler(dgvGames_CellClick);


            //Create a new list of custom games data
            //_customGames = new List<DataSet>();
        }

        private void fillExperimentData()
        {
            DataTable experiments = _dsExperiments.Tables[EXPERIMENTS_TABLE];
            for (int i = 0; i < experiments.Rows.Count; ++i)
            {
                //MessageBox.Show(experiments.Rows.Count.ToString());
                int newRoxIndex = dgvGames.Rows.Add();
                DataGridViewRow row = dgvGames.Rows[newRoxIndex];

                row.Cells[EXPERIMENT].Value = experiments.Rows[i][EXPERIMENT].ToString();
                
                row.Cells[DETAILS].Tag = experiments.Rows[i][DETAILS].ToString();
                row.Cells[DETAILS].Value = "Show";

               // row.Cells[COMMENTS].Value = experiments.Rows[i][COMMENTS].ToString();
            }
        }

        private void dgvGames_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells. 
            if (e.RowIndex < 0 || e.ColumnIndex !=
                dgvGames.Columns[DETAILS].Index) return;

            //Ignore clicks before running the experiment
            if (dgvGames[DETAILS, e.RowIndex].Value.ToString().Length == 0)
                return;

            // Retrieve the game detail file path
            string path = dgvGames[DETAILS, e.RowIndex].Tag.ToString();

            frmAnalyzer frm = new frmAnalyzer(path);
            frm.ShowDialog();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            List<DataSet> games = new List<DataSet>(dgvGames.Rows.Count);
            for (int i = 0; i < dgvGames.Rows.Count; ++i)
            {
                DataGridViewCell buttonCell = dgvGames.Rows[i].Cells[DETAILS];

                if (buttonCell.Value.ToString().Length == 0)
                    games.Add((DataSet)buttonCell.Tag);
            }

            MultiGameExperiment exp = new MultiGameExperiment(games);

            //#region Creating logger
            //Create Game Logger
            //String stamp = DateTime.Now.ToString("yyyy.MM.dd-HH.mm.ss");
            //String fileName = "MultiGame" + stamp + ".xml";
            //XmlLogger logger = new XmlLogger(fileName);

            //exp.Logger = logger;
            //#endregion

            exp.runExperiment();

            //Get results files
            List<String> files = exp.ResultsFiles;

            int j = 0;
            for (int i = 0; i < dgvGames.Rows.Count; ++i)
            {
                DataGridViewCell buttonCell = dgvGames.Rows[i].Cells[DETAILS];
                if (buttonCell.Value.ToString().Length == 0)
                {
                    dgvGames.Rows[i].Cells[DETAILS].Value = "Show";
                    dgvGames.Rows[i].Cells[DETAILS].Tag = files[j++]; //3 * i; //Put index of the game data table
                }
            }
        }

        private void btnFromNewGameGroup_Click(object sender, EventArgs e)
        {
            String tag = ((Button)sender).Tag.ToString();

            if (tag.Equals("Add"))
            {
                frmLauncher frm = new frmLauncher("Game" + _gameNameCounter.ToString());

                if (frm.ShowDialog() == DialogResult.OK)
                {
                    ++_gameNameCounter;
                    DataSet dsGameData = frm.getGameData();

                    int newRowIndex = dgvGames.Rows.Add();
                    DataGridViewRow row = dgvGames.Rows[newRowIndex];

                    row.Cells[EXPERIMENT].Value = txtName.Text;
                    //row.Cells[1].Value = cbGameType.Text;
                    row.Cells[DETAILS].Value = "";
                    
                    row.Cells[DETAILS].Tag = dsGameData;
                    row.Cells[COMMENTS].Value = txtComments.Text;
                    
                    //_customGames.Add(dsGameData);
                }
            }
        }

        private void dgvGames_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData != Keys.Delete)
                return;

            if (dgvGames.SelectedRows.Count == 0)
                return;

            dgvGames.Rows.Remove(dgvGames.SelectedRows[0]);
        }
    }
}
